package SMSGUI;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFReader;

public class readDBF {

	public static HashMap<String, Double[]> read(String file) {
		try {

			// create a DBFReader object
			//
			InputStream inputStream = new FileInputStream(file); // take dbf
																	// file as
																	// program
																	// argument
			DBFReader reader = new DBFReader(inputStream);

			// get the field count if you want for some reasons like the
			// following
			//
			int numberOfFields = reader.getFieldCount();

			// use this count to fetch all field information
			// if required
			//
						// Now, lets us start reading the rows
			//
			Object[] rowObjects;
			HashMap<String, Double[]> cityCoords = new HashMap<String, Double[]>();
			for (int i = 0; i < reader.getRecordCount(); i++) {
				try {
					rowObjects = reader.nextRecord();

					// naam = 0
					// long = 9
					// lat = 10

					Double[] coords = new Double[2];
					if (rowObjects[9] != null && rowObjects[10] != null) {
						coords[0] = (Double) rowObjects[9];
						coords[1] = (Double) rowObjects[10];
						cityCoords.put(("" + rowObjects[0]).replace(" ", "").toLowerCase(),coords);
					}
				} catch (DBFException e) {

				}

			}

			// By now, we have itereated through all of the rows

			inputStream.close();
			return cityCoords;
		} catch (DBFException e) {

			System.out.println("BDFEXCEPTION1 " + e.getMessage());
		} catch (IOException e) {

			System.out.println("BDFEXCEPTION2 " + e.getMessage());
		}
		return null;
	}

}
